﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using PlanB.Core.Configuration;
using PlanB.ShareDove.ConferenceOrganization.BusinessLayer;
using PlanB.ShareDove.ConferenceOrganization.BusinessLayer.DataLayerInterface.Entities;

using System.Security.Principal;
using PlanB.Core.Logging;
using Microsoft.SharePoint;

namespace PlanB.ShareDove.ConferenceOrganization.TestConsole
{
    class Program
    {
        static string m_SiteName;
        static string m_WebName;
        static string m_DataLayerImplementationAssembly;
        static string m_DataLayerImplementationClass;

        private const string SP_DATA_LAYER_ASSEMBLY_PATH = @"C:\ProDev\PlanB\ShareDove\ConferenceOrganization\DataLayer\SharePointDataLayer\bin\Debug\PlanB.ShareDove.ConferenceOrganization.SharePointDataLayer.dll";
        private const string MOCK_DATA_LAYER_ASSEMBLY_PATH = @"C:\ProDev\PlanB\ShareDove\ConferenceOrganization\DataLayer\MockDataLayer\bin\Debug\PlanB.ShareDove.ConferenceOrganization.MockDataLayer.dll";


        static void Main(string[] args)
        {

            //
            //copy data layer implementations in the output folder
            //string cd = Directory.GetCurrentDirectory();
            //System.IO.File.Copy(MOCK_DATA_LAYER_ASSEMBLY_PATH, Path.Combine(cd, Path.GetFileName(MOCK_DATA_LAYER_ASSEMBLY_PATH)), true);
            //System.IO.File.Copy(SP_DATA_LAYER_ASSEMBLY_PATH, Path.Combine(cd, Path.GetFileName(SP_DATA_LAYER_ASSEMBLY_PATH)), true);

            m_SiteName = ConfigurationManager.AppSettings["spsite"];
            m_WebName = ConfigurationManager.AppSettings["spweb"];

            m_DataLayerImplementationAssembly = ConfigurationManager.AppSettings["datalayerimplementation"];
            m_DataLayerImplementationClass = ConfigurationManager.AppSettings["datalayerclass"];

            string command = "___";

            Console.WriteLine("ShareDove Conference Portal Console:" + Environment.NewLine);

            while (command.Trim().ToUpper() != "EXIT")
            {

                command = Console.ReadLine();
                command = command.Trim().ToUpper();

                switch (command)
                {
                    case "DUMMY":
                        dummyTest();
                        break;
                    case "SESSIONS":
                        getSessionsForDay();
                        break;
                    case "SESSIONSNOCONTEXT":
                    case "SNC":
                        getSessionsForDayWithoutContext();
                        break;
                    case "CONF":
                        getConfiguration();
                        break;
                    default:
                        break;
                }
            }

        }

        private static void getConfiguration()
        {
            Console.WriteLine("Gettint custom configuration from app.config");

            PlanB.Core.Logging.LoggingConfiguration loggingSection = (PlanB.Core.Logging.LoggingConfiguration)ConfigurationManager.GetSection("PlanB/Logging");

            if (loggingSection != null)
            {
                ILogger log = LogFactory.InstantiateLog(loggingSection);

                log.EnsureArea("TestAreaAdis");
            }


            //Console.ReadLine();

        }


        /// <summary>
        /// Get all sessions for 19.10.2011
        /// </summary>
        private static void getSessionsForDay()
        {

            //TODO: Change here to fully qualified assembly name - go away from own implementation
            ConfigurationInformation configuration = new ConfigurationInformation()
            {
                DataLayerImplementationAssembly = m_DataLayerImplementationAssembly,
                DataLayerImplementationClassName = m_DataLayerImplementationClass
            };
            ConferenceOrganizationBusinessProcesses target = new ConferenceOrganizationBusinessProcesses(configuration);
            DateTime day = new DateTime(2011, 10, 19);
            IEnumerable<ConferenceSession> sessions;
            object context = "";
            sessions = target.GetSessionsForDay(context, day);


            if (sessions != null)
            {
                foreach (ConferenceSession session in sessions)
                {
                    Console.WriteLine(string.Format("{0} - {1} - {2}", session.Slot.ToShortDateString() + " " + session.Slot.ToShortTimeString(), session.Track.Name, session.Title));
                }
            }


        }


        /// <summary>
        /// Get all sessions for 19.10.2011 but let the data layer create context
        /// </summary>
        private static void getSessionsForDayWithoutContext()
        {
            Console.WriteLine("SESSIONS " + m_WebName);
            Console.WriteLine("-------------------------------" + Environment.NewLine);

            //TODO: Go away here from the own implementation, use the fully qualified assembly names
            ConfigurationInformation configuration = new ConfigurationInformation()
            {
                DataLayerImplementationAssembly = m_DataLayerImplementationAssembly,
                DataLayerImplementationClassName = m_DataLayerImplementationClass
            };

            ConferenceOrganizationBusinessProcesses target = new ConferenceOrganizationBusinessProcesses(configuration);
            DateTime day = new DateTime(2011, 10, 19);

            IEnumerable<ConferenceSession> sessions;
            object context = string.Format("{0};{1}", m_SiteName, m_WebName);
            sessions = target.GetSessionsForDay(context, day);


            if (sessions != null)
            {
                foreach (ConferenceSession session in sessions)
                {
                    Console.WriteLine(string.Format("{0} - {1} - {2}", session.Slot.ToShortDateString() + " " + session.Slot.ToShortTimeString(), session.Track.Name, session.Title));
                }
            }

        }

        //TODO: Change configurartion of web service to fully qualified name, go away from own implementation

        /// <summary>
        /// Dummy test
        /// </summary>
        private static void dummyTest()
        {
            try
            {
                using (SPSite site = new SPSite(m_SiteName))
                {
                    using (SPWeb web = site.OpenWeb(m_WebName))
                    {
                        Console.WriteLine("dummy test " + web.Url);
                        Console.WriteLine("-------------------------------" + Environment.NewLine);


                        Console.WriteLine(Environment.NewLine + Environment.NewLine);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message);
            }


        }


    }
}